﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dao;
using Dao.Intermediarios;

namespace _InformaticaIndustrial_DerivdosVinicos.Interfaz
{
    public partial class ABMDemanda : Form
    {
        IPDemanda ipDem = new IPDemanda();
        IPProducto ipProd = new IPProducto();
        IList<Demanda> listaDem;
        Demanda demanda;
        List<Demanda> demandaList = new List<Demanda>();

        public ABMDemanda(Principal principal)
        {
            InitializeComponent();
            cargarCombos();
        }

        public void cargarCombos()
        {
            comboBoxProducto.DataSource = ipProd.getAll<Producto>();
        }

        private void btAgregar_Click(object sender, EventArgs e)
        {
            demanda = new Demanda();

            try
            {
                DataGridViewRow fila = new DataGridViewRow();

                DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                cell = new DataGridViewTextBoxCell();
                //CARGA EL PERIODO
                string per = cbPeriodo.SelectedItem.ToString();
                cell.Value = per;
                fila.Cells.Add(cell);
                demanda.periodo = Convert.ToInt16(per);
                dgDemanda.Rows.Add(fila);

                demanda.periodo = Convert.ToInt16(per);
                Producto prod = ipProd.getByCriterio<Producto>("IdProducto", "=", comboBoxProducto.SelectedValue.ToString()).First<Producto>();

                demanda.IdProducto = prod.IdProducto;

                demandaList.Add(demanda);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Debe elegir un periodo.");
            }
        }

        private void btSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxProducto_SelectedIndexChanged(object sender, EventArgs e)
        {


        }
        private void comboBoxProducto_SelectedValueChanged(object sender, EventArgs e)
        {
            dgDemanda.Rows.Clear(); 
        }

        private void btBuscar_Click(object sender, EventArgs e)
        {
            int valor = Convert.ToInt16(comboBoxProducto.SelectedValue);
            listaDem = ipDem.getCriterioById<Demanda>("", "", valor);

            foreach (var d in listaDem)
            {
                DataGridViewRow fila = new DataGridViewRow();

                DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                cell = new DataGridViewTextBoxCell();

                cell.Value = d.periodo;
                fila.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = d.demandaEstimada;
                fila.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = d.demandaReal;
                fila.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = d.error;
                fila.Cells.Add(cell);

                dgDemanda.Rows.Add(fila);

            }

        }

        private void dgDemanda_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgDemanda_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            double errorAbsoluto = 0;
            double errorReal = 0;
            int i=dgDemanda.RowCount;
          
            demanda.demandaReal = Convert.ToDouble(dgDemanda.Rows[i - 2].Cells["demReal"].Value);
            demanda.demandaEstimada = Convert.ToDouble(dgDemanda.Rows[i - 2].Cells["demPredecida"].Value);
            //demanda.error =
            errorReal = Convert.ToDouble(dgDemanda.Rows[i - 2].Cells["demReal"].Value) - Convert.ToDouble(dgDemanda.Rows[i - 2].Cells["demPredecida"].Value);
            errorAbsoluto = (errorReal > 0) ? (errorReal) : (-1 * errorReal);
            dgDemanda.Rows[i - 2].Cells["Error"].Value = errorAbsoluto;
            demanda.error = errorAbsoluto;

            ipDem.save<Demanda>(demanda);
            Intermediario.confirmarCambios();
        }

        private void btGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (var item in demandaList)
                {
                    
                }
                
                 MessageBox.Show("La Demanda ha sido creada.");
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
    }

